package com.ujoku.payment.chinaBank;

import com.labillusion.core.platform.crypto.MD5;
import com.labillusion.core.util.HttpUtils;
import com.ujoku.domain.OrderPayment;
import org.springframework.stereotype.Repository;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Greg.Chen on 14-12-5.
 */
public class ChinaBankCore {

    public static Map<String,String> buildRequestMap(HttpServletRequest request) throws UnsupportedEncodingException {

        Map<String,String> map = new HashMap<>();

        String v_oid = request.getParameter("v_oid");		// 订单号
        String v_pmode = request.getParameter("v_pmode");		// 支付方式中文说明，如"中行长城信用卡"
        v_pmode = new String(v_pmode.getBytes( "ISO8859_1"), "GBK");

        String v_pstatus = request.getParameter("v_pstatus");	// 支付结果，20支付完成；30支付失败；
        String v_pstring = request.getParameter("v_pstring");	// 对支付结果的说明，成功时（v_pstatus=20）为"支付成功"，支付失败时（v_pstatus=30）为"支付失败"
        v_pstring = new String(v_pstring.getBytes( "ISO8859_1"), "GBK");

        String v_amount = request.getParameter("v_amount");		// 订单实际支付金额
        String v_moneytype = request.getParameter("v_moneytype");	// 币种
        String v_md5str = request.getParameter("v_md5str");		// MD5校验码

        map.put("v_oid",v_oid);
        map.put("v_pmode",v_pmode);
        map.put("v_pstatus",v_pstatus);
        map.put("v_pstring",v_pstring);
        map.put("v_amount",v_amount);
        map.put("v_moneytype",v_moneytype);
        map.put("v_md5str",v_md5str);
        map.put("text",map.get("v_oid")+map.get("v_pstatus")+map.get("v_amount")+map.get("v_moneytype") + ChinaBankConfig.key);

        return map;
    }


    public static Map<String,String> buildRequestMap(HttpServletRequest request, OrderPayment payment, String returnUrl) {

        Map<String,String> map = new HashMap<>();

 /*       String v_url = HttpUtils.getBasePath(request) + "/chinaBank/return";*/
        String v_url = returnUrl;
        String remark2 = HttpUtils.getBasePath(request) + "/chinaBank/notify";
        String v_oid = payment.getOrderId();
        String v_amount = String.valueOf(payment.getAmount());
        String v_moneytype ="CNY";
        String text = v_amount+v_moneytype+v_oid+ChinaBankConfig.v_mid+v_url+ChinaBankConfig.key;
        String v_md5info = MD5.encrypt(text).toUpperCase();

        map.put("v_mid", ChinaBankConfig.v_mid);
        map.put("v_url", v_url);
        map.put("v_oid", v_oid);
        map.put("v_amount", v_amount);
        map.put("v_moneytype", v_moneytype);
        map.put("v_md5info", v_md5info);
        map.put("remark2", remark2);

        return map;
    }
}
